//
//  BoomTower.m
//  TowerDefence
//
//  Created by  on 5/29/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BoomTower.h"
#import "PlayScreen.h"
#import "Enemy.h"

@implementation BoomTower

-(id)initBoomTowerWith:(GameLayer *)_gameLayer {
    self = [super initTowerObjectWith:_gameLayer];
    [self loadBoomTower];
    maxRadiusFireTarget = 150.0f;
    buyPrice = BOOM_TOWER_CONSTRUCTION_PRICE;
//    CGSize size=CGSizeMake(maxRadiusFireTarget, maxRadiusFireTarget);
    //range.scale=maxRadiusFireTarget*2/range.contentSize.width;
//    NSLog(@"width %f",range.contentSize.width);
    return self;
}

-(void)loadBoomTower {    
    temp = [[CCSprite alloc]initWithSpriteFrameName:@"boomTower0.png"];
    menuItemSpriteBoomTower = [CCMenuItemSprite itemFromNormalSprite:temp selectedSprite:nil target:self selector:@selector(showOption)];
    
    menuBoomTower = [CCMenu menuWithItems:menuItemSpriteBoomTower, nil];
    menuBoomTower.position = ccp(0, 0);
    [spriteBody addChild:menuBoomTower];
}

-(void)fireTarget{
    if([handler.listGameObject containsObject:targetLocked]) {
        bullet = [[StoneBullet alloc]initStoneBulletWith:self.handler tower:self];
        bullet.spriteBody.position = ccp(spriteBody.position.x + 5, spriteBody.position.y + 20);
        bullet.spriteBody.anchorPoint = ccp(0.5, 0);
        
        CGPoint targetPos;
        if(((Enemy *)targetLocked).inBattle) {
            targetPos = targetLocked.spriteBody.position;
        } else {
            targetPos = [(Enemy *)targetLocked getPositionAfterTime:bullet.velocity];
        }
        targetPos = ccp(targetPos.x, targetPos.y + targetLocked.spriteBody.contentSize.height / 2 - 5);
        
        if(!CGPointEqualToPoint(targetPos, targetLocked.spriteBody.position)) {
            [(PlayScreen *)handler fireEnemy:targetPos tower:self bullet:bullet];
        }
        
    } else {
        isShooting = NO;
    }
}

-(void)shootTargetAnimation { 
    if([handler.listGameObject containsObject:targetLocked]) {    
        CCAnimation *beforeShootAnimation = [self loadAnimationFromCacheByName:@"boomTower_beforeShoot" withCount:9 delay:0.05f];
        CCAnimation *afterShootAnimation = [self loadAnimationFromCacheByName:@"boomTower_afterShoot" withCount:4 delay:0.05f];
        
        CCCallFunc *fireEnemyCallback = [CCCallFunc actionWithTarget:self selector:@selector(fireTarget)];
        
        [temp runAction:[CCSequence actions:[CCAnimate actionWithAnimation:beforeShootAnimation restoreOriginalFrame:NO], fireEnemyCallback, [CCAnimate actionWithAnimation:afterShootAnimation restoreOriginalFrame:NO], nil]];
    }
}

-(void)hitTargetOfBullet:(GameObject *)_bullet {
    [super hitTargetOfBullet:_bullet];
    if([((PlayScreen *)handler).listGameObject containsObject:targetLocked]){
        for (int i=0;i<handler.listGameObject.count;i++){
            GameObject *gameObject=[handler.listGameObject objectAtIndex:i];
            if([gameObject isKindOfClass:[Enemy class]] && ccpDistance(gameObject.spriteBody.position, targetLocked.spriteBody.position)<=40 && ![gameObject  isEqual:targetLocked]){
                ((Enemy *) gameObject).HP -= ((BulletObject*)_bullet).damage;
                [((Enemy *) gameObject) animationHit];
                [((Enemy *) gameObject) updateBloodMeter];
            }
        }
    }
}

-(void)dealloc {        
    [temp stopAllActions];
    [temp removeAllChildrenWithCleanup:YES];
    [temp removeFromParentAndCleanup:YES];
    [temp release];
    
    [self stopAllActions];
    [self removeAllChildrenWithCleanup:YES];
    [self removeFromParentAndCleanup:YES];
    
    [self removeAnimationInFrameCache:@"boomTower_beforeShoot"];
    [self removeAnimationInFrameCache:@"boomTower_afterShoot"];
    
    [super dealloc];
}

@end
